package com.qyer.util.log.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.hadoop.io.Text;

import com.qyer.util.log.common.Constants;
import com.qyer.util.log.entity.TrackLog;
import com.qyer.util.log.service.LogService;
import com.qyer.util.log.service.impl.LogServiceImpl;

public class TrackSplitUtils {

	//清洗日志工具类
	LogService lli = (LogService)LogFatory.getInstance(LogServiceImpl.class.getName());
	
	private TrackSplitUtils() {
	}

	private static final TrackSplitUtils instance = new TrackSplitUtils();

	public static TrackSplitUtils getInstance() {
		return instance;
	}

	public List<List<String>> getSplitVisit(Text textLine) {
		List<List<String>> listText = new ArrayList<List<String>>();
		List<String> listSession = new ArrayList<String>();
		int intSessionNum = 1, intNextEndNum = 1;
		StringTokenizer commaToker = new StringTokenizer(textLine.toString(), Constants.NFS);
		TrackLog tracklog=null;
		int i=0;
		while (commaToker.hasMoreTokens()) {
			String strLine = commaToker.nextToken();
			
			//判断一下device_id是否是"000000000000000"
			if(i == 0){
				tracklog = lli.getTrackFormat(strLine);
				if(tracklog == null){
					continue;
				}
				if("000000000000000".equals(tracklog.getUnique_id())){
					return Collections.emptyList();
				}
				i++;
			}
			String[] arrInfo = strLine.split(Constants.FS, -1);
		    try {
				intNextEndNum = strLine.endsWith(Constants.FS) ? intSessionNum : Integer.parseInt(arrInfo[arrInfo.length - 1].trim());
			} catch (Exception e) {
				intNextEndNum = 1;
			}
			if (intSessionNum < intNextEndNum) {
				listText.add(listSession);
				intSessionNum = intNextEndNum;
				listSession = new ArrayList<String>();
			}
			listSession.add(strLine);
		}
		listText.add(listSession);
		return listText;
	}

}
